package com.newdjk.okhttp;

import android.accounts.NetworkErrorException;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.newdjk.okhttp.dialog.LoadDialog;
import com.newdjk.okhttp.entity.BaseEntity;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import javax.net.ssl.SSLHandshakeException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/*
 *  @项目名：  RetrofitRxjavaDemo
 *  @包名：    com.example.huhai.retrofitrxjavademo.net
 *  @文件名:   BaseObserver
 *  @创建者:   EDZ
 *  @创建时间:  2018/11/1 10:58
 *  @描述：
 */
public abstract class BaseObserver<T> implements Observer<BaseEntity<T>> {
    private static final String TAG = "BaseObserver";
    protected Context mContext;
    protected boolean isshowDialog=true;

    public BaseObserver(Context cxt) {
        this.mContext = cxt;
    }
    public BaseObserver(Context cxt,boolean isshowDialog) {
        this.mContext = cxt;
        this.isshowDialog = isshowDialog;
    }
    public BaseObserver() {

    }

    @Override
    public void onSubscribe(Disposable d) {
        onRequestStart();

    }

    @Override
    public void onNext(BaseEntity tBaseEntity) {
        closeProgressDialog();
        Log.d(TAG, tBaseEntity.toString());
        if (tBaseEntity.getCode() == 0) {
            try {
                onSuccees(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                onCodeError(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        Log.d(TAG, "onError: " + e.toString());//这里可以打印错误信息
        closeProgressDialog();;
        try {
            if (e instanceof ConnectException) {
                onFailure(e, "网络链接异常", true);
            } else if (e instanceof TimeoutException) {
                onFailure(e, "网络链接超时", true);
            } else if (e instanceof NetworkErrorException) {
                onFailure(e, "网络链接异常", true);
            } else if (e instanceof UnknownHostException) {
                onFailure(e, "网络链接异常", true);
            } else if (e instanceof SocketTimeoutException) {
                onFailure(e, "网络链接超时", true);
            } else if (e instanceof SSLHandshakeException) {
                onFailure(e, "SLL证书异常", true);
            } else if (e instanceof DataResultException){
                DataResultException resultException = (DataResultException) e ;
                onFailure(e, resultException.getMsg(), false);
            }else if (e instanceof HttpException){
                onFailure(e, "HTTP 404 Not Found", false);
            }else {
                onFailure(e, "网络链接异常", false);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void onComplete() {
        closeProgressDialog();
    }

    /**
     * 返回成功
     *
     * @param t
     * @throws Exception
     */
    protected abstract void onSuccees(BaseEntity<T> t) throws Exception;

    /**
     * 返回成功了,但是code错误
     *
     * @param t
     * @throws Exception
     */
    protected abstract void onCodeError(BaseEntity<T> t) throws Exception;


    /**
     * 返回失败
     *
     * @param e
     * @param isNetWorkError 是否是网络错误
     * @throws Exception
     */
    protected abstract void onFailure(Throwable e, String erroressage, boolean isNetWorkError) throws Exception;

    protected void onRequestStart() {
        showProgressDialog();
    }

    protected void onRequestEnd() {
        closeProgressDialog();
    }

    public void showProgressDialog() {
        if (isshowDialog){
            LoadDialog.show(mContext);
        }

    }

    public void closeProgressDialog() {
        LoadDialog.clear();
    }

}

